﻿using HtmlAgilityPack;
using OpenQA.Selenium.Chrome;
using System.Speech.Synthesis;

class DouyinLiveChatMonitor
{
    private static ChromeDriver driver;
    private static int lastProcessedIndex = 0; // 记录已处理的最新消息索引 

    static void Main(string[] args)
    {
        var synth = new SpeechSynthesizer();
        Prompt prompt = null;

        // 初始化 ChromeDriver 配置 
        var options = new ChromeOptions();
        // options.AddArgument("--headless"); // 无头模式可选
        options.AddArgument("--disable-gpu");
        options.AddArgument("--window-size=1920,1080");

        // 初始化 ChromeDriver
        driver = new ChromeDriver(options);

        try
        {
            // 导航到目标直播间（请替换为实际URL）
            driver.Navigate().GoToUrl("https://live.douyin.com/694933504399");
            Console.WriteLine("已打开直播间，开始监控聊天内容...");

            // 等待页面加载
            Thread.Sleep(5000);

            // 持续监控新消息
            while (true)
            {
                try
                {
                    string htmlContent = driver.PageSource; // 获取HTML源码 
                    var htmlDoc = new HtmlDocument();
                    htmlDoc.LoadHtml(htmlContent);
                    var targetDiv = htmlDoc.DocumentNode.SelectSingleNode("//div[contains(@class, 'webcast-chatroom___list')]");
                    if (targetDiv != null && targetDiv.HasChildNodes)
                    {
                        //Name: "class", Value: "gOr3NRD4"
                        foreach (var item in targetDiv.ChildNodes)
                        {
                            if (!item.HasChildNodes)
                                continue;
                            //Name: "style", Value: "position: absolute; top: 0px; left: 0px; width: 100%; transform: translateY(0px);"
                            foreach (var item1 in item.ChildNodes)
                            {
                                if (!item1.HasChildNodes)
                                    continue;
                                //Name: "data-index", Value: "0"
                                foreach (var item2 in item1.ChildNodes)
                                {
                                    if (!item2.HasChildNodes)
                                        continue;

                                    var dataIndex = item2.GetAttributeValue("data-index", "0");
                                    var dataIndexValue = int.Parse(dataIndex);
                                    if (dataIndexValue < lastProcessedIndex)
                                        continue;
                                    lastProcessedIndex = dataIndexValue;
                                    //Value = "webcast-chatroom___item webcast-chatroom___item_new"
                                    foreach (var item3 in item2.ChildNodes)
                                    {
                                        if (!item3.HasChildNodes)
                                            continue;
                                        //Value = "Cl4EfhXg webcast-chatroom___item-wrapper"
                                        foreach (var item4 in item3.ChildNodes)
                                        {
                                            if (!item4.HasChildNodes)
                                                continue;
                                            //Value = "TNg5meqw"
                                            foreach (var item5 in item4.ChildNodes)
                                            {
                                                if (!item5.HasChildNodes)
                                                    continue;

                                                if (item5.ChildNodes.Count < 3)
                                                    continue;

                                                //Value = "cursor: pointer; display: inline-block; height: 20px; vertical-align: middle;"
                                                var alt = "";
                                                var src = "";
                                                foreach (var item7 in item5.ChildNodes[0].ChildNodes)
                                                {
                                                    foreach (var item8 in item7.ChildNodes)
                                                    {
                                                        foreach (var item9 in item8.ChildNodes)
                                                        {
                                                            alt = item9.GetAttributeValue("alt", "");
                                                            src = item9.GetAttributeValue("src", "");
                                                        }
                                                    }
                                                }
                                                //Value = "u2QdU6ht"
                                                var userName = "";
                                                foreach (var item7 in item5.ChildNodes[1].ChildNodes)
                                                {
                                                    userName = item7.InnerText;
                                                }
                                                //Value = "WsJsvMP9"
                                                var content = "";
                                                foreach (var item7 in item5.ChildNodes[2].ChildNodes)
                                                {
                                                    foreach (var item8 in item7.ChildNodes)
                                                    {
                                                        content = item8.InnerText;
                                                    }
                                                }
                                                Console.WriteLine($"{userName}{content}");

                                                if (prompt != null && !prompt.IsCompleted)
                                                    synth.SpeakAsyncCancel(prompt);
                                                prompt = synth.SpeakAsync($"{userName}{content}");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                Thread.Sleep(1000); // 每秒检查一次新消息
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误: {ex.Message}");
        }
        finally
        {
            driver?.Quit();
        }
    }
}